package com.huanghuang.shuadan.manager.user;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huanghuang.shuadan.common.exception.base.BaseException;
import com.huanghuang.shuadan.common.exception.base.BaseExceptionEnum;
import com.huanghuang.shuadan.common.exception.business.BusinessEnum;
import com.huanghuang.shuadan.common.exception.business.BusinessException;
import com.huanghuang.shuadan.common.exception.user.UserException;
import com.huanghuang.shuadan.common.exception.user.UserExceptionEnum;
import com.huanghuang.shuadan.dao.entity.req.user.UserReqDo;
import com.huanghuang.shuadan.dao.entity.res.user.UserResDo;
import com.huanghuang.shuadan.dao.mapper.user.UserDao;
import com.huanghuang.shuadan.manager.user.converter.UserConverter;
import com.huanghuang.shuadan.service.entity.base.PageReqDTO;
import com.huanghuang.shuadan.service.entity.base.Result;
import com.huanghuang.shuadan.service.entity.req.UserRegisterReqDTO;
import com.huanghuang.shuadan.service.entity.req.UsernameReqDTO;
import com.huanghuang.shuadan.service.entity.res.UserResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author suxiaoxiao
 * @Date 2019/7/13 22:21
 * @Version 1.0
 */
@Component
@Slf4j
public class UserManager {

    @Autowired
    private UserDao userDao;

    @Autowired
    private HttpSession session;

    /**
     * 查询所有用户
     * @return
     */
    public Result<UserResDTO> queryAllUser(PageReqDTO pageReqDTO) {
        Result<UserResDTO> result;
        List<UserResDo> userList;
        PageHelper.startPage(pageReqDTO.getPageNum(),pageReqDTO.getPageSize());
        try {
            userList = userDao.selectAllUser();
            log.info("查询所有用户 UserManager.queryAllUser:{}", userList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(BaseExceptionEnum.SYSTEM_BUSY);
        }
        if (userList == null) {
            throw new BusinessException(BusinessEnum.DATA_NOT_EXIST);
        }
        result = new Result(UserConverter.INSTANCE.convert2UserResList(userList));
        return result;
    }

    public Result<UserResDTO> selectUserByUsername(UsernameReqDTO usernameReqDTO) {
        UserResDo userResDo;
        UserReqDo userReqDo = UserConverter.INSTANCE.convert2UserReqDo(usernameReqDTO);
        log.info("查询用户 UserManager.selectUserByUsername [入参]:{}", userReqDo);
        try {
            userResDo = userDao.selectUserByUsername(userReqDo);
            log.info("查询用户 UserManager.selectUserByUsername [出参]:{}", userResDo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(BaseExceptionEnum.SYSTEM_BUSY);
        }
        if (userResDo == null) {
            throw new BusinessException(BusinessEnum.DATA_NOT_EXIST);
        }
        return new Result(UserConverter.INSTANCE.convert2UserResDTO(userResDo));
    }

    public Result<Boolean> registerUser(UserRegisterReqDTO userRegisterReqDTO){
        Result<Boolean> result;
        UserResDo userResDo;

        String captcha = (String)session.getAttribute("captcha");
        if (captcha == null || !userRegisterReqDTO.getCaptcha().equalsIgnoreCase(captcha)) {
            throw new UserException(UserExceptionEnum.USER_CAPTCHA_INCORRECT);
        }

        UserReqDo userReqDo = UserConverter.INSTANCE.convert2UserReqDo(userRegisterReqDTO);
        boolean isRegisterSuccess;
        try {
            log.info("查询用户信息 UserManager.selectUserByUsername [入参]:{}", userReqDo);
            userResDo = userDao.selectUserByUsername(userReqDo);
            log.info("查询用户信息 UserManager.selectUserByUsername [出参]:{}", userRegisterReqDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(BaseExceptionEnum.SYSTEM_BUSY);
        }
        if (userResDo != null) {
            throw new UserException(UserExceptionEnum.USERNAME_EXIST);
        } else {
            userReqDo.setPassword(userRegisterReqDTO.getPassword());
            try {
                log.info("插入用户信息 UserManager.insertUser [入参]:{}", userReqDo);
                isRegisterSuccess = userDao.insertUser(userReqDo);
                log.info("插入用户信息 UserManager.insertUser [出参]:{}", isRegisterSuccess);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BaseException(BaseExceptionEnum.SYSTEM_BUSY);
            }
        }
        result = new Result<>(isRegisterSuccess);
        return result;
    }

    public Result<UserResDTO> login(UserRegisterReqDTO userRegisterReqDTO){
        UserResDo userResDo;
        UserReqDo userReqDo = UserConverter.INSTANCE.convert2UserReqDo(userRegisterReqDTO);
        log.info("查询用户 UserManager.login [入参]:{}", userReqDo);

        try {
            userResDo = userDao.selectUserByUsername(userReqDo);
            log.info("查询用户 UserManager.login [出参]:{}", userResDo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(BaseExceptionEnum.SYSTEM_BUSY);
        }

        if (userResDo == null) {
            throw new UserException(UserExceptionEnum.USERNAME_NOT_FOUND);
        } else {
            String captcha = (String)session.getAttribute("captcha");
            if (captcha == null) {
                throw new UserException(UserExceptionEnum.USER_CAPTCHA_INCORRECT);
            }
            boolean isEquals = userRegisterReqDTO.getCaptcha().equalsIgnoreCase(captcha);
            if (!isEquals) {
                throw new UserException(UserExceptionEnum.USER_CAPTCHA_INCORRECT);
            }
            isEquals = userRegisterReqDTO.getPassword().equals(userResDo.getPassword());
            if (!isEquals) {
                throw new UserException(UserExceptionEnum.USER_PASSWORD_INCORRECT);
            }
        }

        // 设置session
        session.setAttribute("user",userResDo);
        return new Result(UserConverter.INSTANCE.convert2UserResDTO(userResDo));
    }

    public Result<UserResDTO> info(){
        UserResDo userResDo = (UserResDo)session.getAttribute("user");
        return new Result(UserConverter.INSTANCE.convert2UserResDTO(userResDo));
    }

}
